package com.moorror.codegenvel.db.sqlite.dao.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.moorror.codegenvel.db.sqlite.dao.BaseDAO;
import com.moorror.codegenvel.db.sqlite.dao.CodeGenSceneDao;
import com.moorror.codegenvel.db.sqlite.entity.*;
import com.moorror.codegenvel.utils.DateUtils;
import com.moorror.codegenvel.utils.JacksonUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.sql.Connection;
import java.util.List;
import java.util.Set;

/**
 * @Author jiyanghuang
 * @Date 2025/8/21 07:55
 */
public class CodeGenXRenderConnectDaoImpl extends BaseDAO<CodeGenXRenderConnect> implements CodeGenSceneDao<CodeGenXRenderConnect> {

    private static CodeGenXRenderConnectDaoImpl instance;

    private CodeGenXRenderConnectDaoImpl() {

    }

    public static synchronized CodeGenXRenderConnectDaoImpl getInstance() {
        if (null == instance) {
            instance = new CodeGenXRenderConnectDaoImpl();
        }
        return instance;
    }

    private static final String SELECT_SQL = " select id,name,url,username,password,other_config_json,create_time,update_time from codegen_xrender_db_connection ";

    @Override
    public String tableName() {
        return "codegen_xrender_db_connection";
    }

    @Override
    public void insert(Connection conn, CodeGenXRenderConnect[] ary) {
        if (ary == null || ary.length == 0) {
            return;
        }
        //数据库中存在自增列后，SQLite 就会创建一个 sqlite_sequence 表。所有表的自增列都共享这个表，sqlite_sequence 分别维护着每个自增列的当前值，所以自增列的计数也是单独的。
        String sql = "insert into codegen_xrender_db_connection(name,url,username,password,other_config_json,create_time,update_time) values(?,?,?,?,?,?,?)";
        int size = ary.length;
        if (size == 1) {
            CodeGenXRenderConnect po = ary[0];
            update(conn, sql, po.getName(), po.getUrl(), po.getUsername(), po.getPassword(), po.getOtherConfigJson(), po.getCreateTime(), po.getUpdateTime());
        } else {
            Object[][] args = new Object[size][7];
            for (int i = 0; i < size; i++) {
                CodeGenXRenderConnect po = ary[i];
                args[i][0] = po.getName();
                args[i][1] = po.getUrl();
                args[i][2] = po.getUsername();
                args[i][3] = po.getPassword();
                args[i][4] = po.getOtherConfigJson();
                args[i][5] = po.getCreateTime();
                args[i][6] = po.getUpdateTime();
            }
            updateBatch(conn, sql, args);
        }
    }

    @Override
    public CodeGenXRenderConnect insert(Connection conn, CodeGenXRenderConnect po) {
        String sql = "insert into codegen_xrender_db_connection(name,url,username,password, other_config_json,create_time,update_time) values(?,?,?,?,?,?,?)";
        update(conn, sql, po.getName(), po.getUrl(), po.getUsername(), po.getPassword(), po.getOtherConfigJson(), po.getCreateTime(), po.getUpdateTime());
        String sqlSelect = SELECT_SQL + " where id = (select LAST_INSERT_ROWID()) limit 1";
        return getBean(conn, sqlSelect);
    }

    @Override
    public void delete(Connection conn, Integer id) {

    }

    @Override
    public boolean deleteByUnique(Connection conn, CodeGenXRenderConnect updateT) {
        String sql = "delete from codegen_xrender_db_connection where name = ? ";
        return update(conn, sql, updateT.getName()) > 0;
    }

    @Override
    public void update(Connection conn, CodeGenXRenderConnect[] ary) {

    }

    @Override
    public void update(Connection conn, CodeGenXRenderConnect codeGenScene) {

    }

    @Override
    public boolean updateByUnique(Connection conn, CodeGenXRenderConnect updateT) {
        String sql = "update codegen_xrender_db_connection SET name = ?, url = ?, username = ?, password = ?, other_config_json = ?, update_time = ? where id = ? ";
        return update(conn, sql, updateT.getName(), updateT.getUrl(), updateT.getUsername(), updateT.getPassword(), updateT.getOtherConfigJson(), DateUtils.currentTime(), updateT.getId()) > 0;
    }

    @Override
    public CodeGenXRenderConnect findById(Connection conn, Integer id) {
        return null;
    }

    @Override
    public CodeGenXRenderConnect findByUnique(Connection conn, CodeGenXRenderConnect queryT) {
        String sql = SELECT_SQL + " where name = ? ";
        List<CodeGenXRenderConnect> re = queryList(conn, sql, queryT.getName());
        if (CollectionUtils.isEmpty(re)) {
            return null;
        }
        return re.get(0);
    }

    @Override
    public List<CodeGenXRenderConnect> searchLike(Connection conn, String searchValue) {
        searchValue = searchValue + "%";
        String sql = SELECT_SQL + "where name Like ?  LIMIT 20";
        List<CodeGenXRenderConnect> re = queryList(conn, sql, searchValue);
        if(CollectionUtils.isEmpty(re)){
            return re;
        }
        Set<String> scene = Sets.newHashSet();
        for(CodeGenXRenderConnect codeGenScene : re){
            scene.add(codeGenScene.getName());
        }

        return re;
    }

    @Override
    public List<CodeGenXRenderConnect> searchLikeMTM(String searchValue) {
//        return manyToMany(searchValue,
//                CodeGenXRenderConnectDaoImpl.getInstance(),
//                CodeGenXRenderConnect::getName,
//                CodeGenXRenderConnectDaoImpl.getInstance(),
//                CodeGenSceneTemplate::getTemplate,
//                CodeGenSceneTemplate::getScene,
//                CodeGenTemplateDaoImpl.getInstance(),
//                CodeGenTemplate::getName,
//                CodeGenXRenderConnectDaoImpl::convertFill);
        return null;
    }

    @Override
    public List<CodeGenXRenderConnect> searchLikeOneTM(String searchValue) {
//        return oneToMany(searchValue,
//                CodeGenXRenderConnectDaoImpl.getInstance(),
//                (m -> String.valueOf(m.getId())),
//                CodeGenXRenderTableConfigDaoImpl.getInstance(),
//                (n -> {
//                    CodeGenXRenderTableConfig nR = (CodeGenXRenderTableConfig) n;
//                    return String.valueOf(nR.getConnectionId());
//                }));
        return Lists.newArrayList();
    }

    @Override
    public List<CodeGenXRenderConnect> searchLikeOneTMTM(String searchValue) {
        return oneToManyToMany(searchValue,
                CodeGenXRenderConnectDaoImpl.getInstance(),
                (m -> String.valueOf(m.getId())),
                CodeGenXRenderTableDaoImpl.getInstance(),
                CodeGenXRenderTable::getKf,
                (n -> String.valueOf(n.getId())),
                CodeGenXRenderTableConfigDaoImpl.getInstance(),
                CodeGenXRenderTableConfig::getKf
        );
    }

    @Override
    public void exchangeShowOrder(Connection conn, Integer showOrder1, Integer showOrder2) {

    }

    public static void convertFill(CodeGenSceneTemplate codeGenSceneTemplate, CodeGenTemplate codeGenTemplate) {
        if (null == codeGenSceneTemplate || null == codeGenSceneTemplate) {
            return;
        }
        if (StringUtils.isNotEmpty(codeGenSceneTemplate.getJsonSceneTemplate())) {
            CodeGenTemplate codeGenTemplateJsonFrom = JacksonUtils.toJavaObject(codeGenSceneTemplate.getJsonSceneTemplate(), CodeGenTemplate.class);
            codeGenTemplate.fillJsonValue(codeGenTemplateJsonFrom);
        }
    }
}
